package com.iyoyi.skinlib.widget;

import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;

import java.util.Map;

abstract class AbstractViewFactory<T> {
    public final void update(View view, Resources resources, Map<String, Integer> table) {
        for (Map.Entry<String, Integer> entry : table.entrySet()) {
            final int id = entry.getValue();
            final String name = entry.getKey();

            ViewGroup.LayoutParams params;
            switch (name) {
                case "layout_width":
                    params = view.getLayoutParams();
                    params.width = view.getResources().getDimensionPixelSize(id);
                    view.setLayoutParams(params);
                    break;

                case "layout_height":
                    params = view.getLayoutParams();
                    params.height = view.getResources().getDimensionPixelSize(id);
                    view.setLayoutParams(params);
                    break;

                case "background":
                    view.setBackground(ContextCompat.getDrawable(view.getContext(), id));
                    break;

                default:
                    onAttributeUpdate((T) view, resources, name, id);
            }
        }
    }

    abstract void onAttributeUpdate(T view, Resources resources, String name, int id);
}
